<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: mainwindow.cpp Example File (mainwindows/dockwidgets/mainwindow.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><sup><sup>mainwindows/dockwidgets/mainwindow.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &lt;QtGui&gt;

 #include &quot;mainwindow.h&quot;

 MainWindow::MainWindow()
 {
     textEdit = new QTextEdit;
     setCentralWidget(textEdit);

     createActions();
     createMenus();
     createToolBars();
     createStatusBar();
     createDockWindows();

     setWindowTitle(tr(&quot;Dock Widgets&quot;));

     newLetter();
 }

 void MainWindow::newLetter()
 {
     textEdit-&gt;clear();

     QTextCursor cursor(textEdit-&gt;textCursor());
     cursor.movePosition(QTextCursor::Start);
     QTextFrame *topFrame = cursor.currentFrame();
     QTextFrameFormat topFrameFormat = topFrame-&gt;frameFormat();
     topFrameFormat.setPadding(16);
     topFrame-&gt;setFrameFormat(topFrameFormat);

     QTextCharFormat textFormat;
     QTextCharFormat boldFormat;
     boldFormat.setFontWeight(QFont::Bold);
     QTextCharFormat italicFormat;
     italicFormat.setFontItalic(true);

     QTextTableFormat tableFormat;
     tableFormat.setBorder(1);
     tableFormat.setCellPadding(16);
     tableFormat.setAlignment(Qt::AlignRight);
     cursor.insertTable(1, 1, tableFormat);
     cursor.insertText(&quot;The Firm&quot;, boldFormat);
     cursor.insertBlock();
     cursor.insertText(&quot;321 City Street&quot;, textFormat);
     cursor.insertBlock();
     cursor.insertText(&quot;Industry Park&quot;);
     cursor.insertBlock();
     cursor.insertText(&quot;Some Country&quot;);
     cursor.setPosition(topFrame-&gt;lastPosition());
     cursor.insertText(QDate::currentDate().toString(&quot;d MMMM yyyy&quot;), textFormat);
     cursor.insertBlock();
     cursor.insertBlock();
     cursor.insertText(&quot;Dear &quot;, textFormat);
     cursor.insertText(&quot;NAME&quot;, italicFormat);
     cursor.insertText(&quot;,&quot;, textFormat);
     for (int i = 0; i &lt; 3; ++i)
         cursor.insertBlock();
     cursor.insertText(tr(&quot;Yours sincerely,&quot;), textFormat);
     for (int i = 0; i &lt; 3; ++i)
         cursor.insertBlock();
     cursor.insertText(&quot;The Boss&quot;, textFormat);
     cursor.insertBlock();
     cursor.insertText(&quot;ADDRESS&quot;, italicFormat);
 }

 void MainWindow::print()
 {
     QTextDocument *document = textEdit-&gt;document();
     QPrinter printer;

     QPrintDialog *dlg = new QPrintDialog(&amp;printer, this);
     if (dlg-&gt;exec() != QDialog::Accepted)
         return;

     document-&gt;print(&amp;printer);

     statusBar()-&gt;showMessage(tr(&quot;Ready&quot;), 2000);
 }

 void MainWindow::save()
 {
     QString fileName = QFileDialog::getSaveFileName(this,
                         tr(&quot;Choose a file name&quot;), &quot;.&quot;,
                         tr(&quot;HTML (*.html *.htm)&quot;));
     if (fileName.isEmpty())
         return;
     QFile file(fileName);
     if (!file.open(QFile::WriteOnly | QFile::Text)) {
         QMessageBox::warning(this, tr(&quot;Dock Widgets&quot;),
                              tr(&quot;Cannot write file %1:\n%2.&quot;)
                              .arg(fileName)
                              .arg(file.errorString()));
         return;
     }

     QTextStream out(&amp;file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
     out &lt;&lt; textEdit-&gt;toHtml();
     QApplication::restoreOverrideCursor();

     statusBar()-&gt;showMessage(tr(&quot;Saved '%1'&quot;).arg(fileName), 2000);
 }

 void MainWindow::undo()
 {
     QTextDocument *document = textEdit-&gt;document();
     document-&gt;undo();
 }

 void MainWindow::insertCustomer(const QString &amp;customer)
 {
     if (customer.isEmpty())
         return;
     QStringList customerList = customer.split(&quot;, &quot;);
     QTextDocument *document = textEdit-&gt;document();
     QTextCursor cursor = document-&gt;find(&quot;NAME&quot;);
     if (!cursor.isNull()) {
         cursor.beginEditBlock();
         cursor.insertText(customerList.at(0));
         QTextCursor oldcursor = cursor;
         cursor = document-&gt;find(&quot;ADDRESS&quot;);
         if (!cursor.isNull()) {
             for (int i = 1; i &lt; customerList.size(); ++i) {
                 cursor.insertBlock();
                 cursor.insertText(customerList.at(i));
             }
             cursor.endEditBlock();
         }
         else
             oldcursor.endEditBlock();
     }
 }

 void MainWindow::addParagraph(const QString &amp;paragraph)
 {
     if (paragraph.isEmpty())
         return;
     QTextDocument *document = textEdit-&gt;document();
     QTextCursor cursor = document-&gt;find(tr(&quot;Yours sincerely,&quot;));
     if (cursor.isNull())
         return;
     cursor.beginEditBlock();
     cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor, 2);
     cursor.insertBlock();
     cursor.insertText(paragraph);
     cursor.insertBlock();
     cursor.endEditBlock();

 }

 void MainWindow::about()
 {
    QMessageBox::about(this, tr(&quot;About Dock Widgets&quot;),
             tr(&quot;The &lt;b&gt;Dock Widgets&lt;/b&gt; example demonstrates how to &quot;
                &quot;use Qt's dock widgets. You can enter your own text, &quot;
                &quot;click a customer to add a customer name and &quot;
                &quot;address, and click standard paragraphs to add them.&quot;));
 }

 void MainWindow::createActions()
 {
     newLetterAct = new QAction(QIcon(&quot;:/images/new.png&quot;), tr(&quot;&amp;New Letter&quot;),
                                this);
     newLetterAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
     newLetterAct-&gt;setStatusTip(tr(&quot;Create a new form letter&quot;));
     connect(newLetterAct, SIGNAL(triggered()), this, SLOT(newLetter()));

     saveAct = new QAction(QIcon(&quot;:/images/save.png&quot;), tr(&quot;&amp;Save...&quot;), this);
     saveAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
     saveAct-&gt;setStatusTip(tr(&quot;Save the current form letter&quot;));
     connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

     printAct = new QAction(QIcon(&quot;:/images/print.png&quot;), tr(&quot;&amp;Print...&quot;), this);
     printAct-&gt;setShortcut(tr(&quot;Ctrl+P&quot;));
     printAct-&gt;setStatusTip(tr(&quot;Print the current form letter&quot;));
     connect(printAct, SIGNAL(triggered()), this, SLOT(print()));

     undoAct = new QAction(QIcon(&quot;:/images/undo.png&quot;), tr(&quot;&amp;Undo&quot;), this);
     undoAct-&gt;setShortcut(tr(&quot;Ctrl+Z&quot;));
     undoAct-&gt;setStatusTip(tr(&quot;Undo the last editing action&quot;));
     connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));

     quitAct = new QAction(tr(&quot;&amp;Quit&quot;), this);
     quitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
     quitAct-&gt;setStatusTip(tr(&quot;Quit the application&quot;));
     connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));

     aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
     aboutAct-&gt;setStatusTip(tr(&quot;Show the application's About box&quot;));
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

     aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
     aboutQtAct-&gt;setStatusTip(tr(&quot;Show the Qt library's About box&quot;));
     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 }

 void MainWindow::createMenus()
 {
     fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
     fileMenu-&gt;addAction(newLetterAct);
     fileMenu-&gt;addAction(saveAct);
     fileMenu-&gt;addAction(printAct);
     fileMenu-&gt;addSeparator();
     fileMenu-&gt;addAction(quitAct);

     editMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Edit&quot;));
     editMenu-&gt;addAction(undoAct);

     viewMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;View&quot;));

     menuBar()-&gt;addSeparator();

     helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
     helpMenu-&gt;addAction(aboutAct);
     helpMenu-&gt;addAction(aboutQtAct);
 }

 void MainWindow::createToolBars()
 {
     fileToolBar = addToolBar(tr(&quot;File&quot;));
     fileToolBar-&gt;addAction(newLetterAct);
     fileToolBar-&gt;addAction(saveAct);
     fileToolBar-&gt;addAction(printAct);

     editToolBar = addToolBar(tr(&quot;Edit&quot;));
     editToolBar-&gt;addAction(undoAct);
 }

 void MainWindow::createStatusBar()
 {
     statusBar()-&gt;showMessage(tr(&quot;Ready&quot;));
 }

 void MainWindow::createDockWindows()
 {
     QDockWidget *dock = new QDockWidget(tr(&quot;Customers&quot;), this);
     dock-&gt;setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
     customerList = new QListWidget(dock);
     customerList-&gt;addItems(QStringList()
             &lt;&lt; &quot;John Doe, Harmony Enterprises, 12 Lakeside, Ambleton&quot;
             &lt;&lt; &quot;Jane Doe, Memorabilia, 23 Watersedge, Beaton&quot;
             &lt;&lt; &quot;Tammy Shea, Tiblanka, 38 Sea Views, Carlton&quot;
             &lt;&lt; &quot;Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal&quot;
             &lt;&lt; &quot;Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston&quot;
             &lt;&lt; &quot;Sally Hobart, Tiroli Tea, 67 Long River, Fedula&quot;);
     dock-&gt;setWidget(customerList);
     addDockWidget(Qt::RightDockWidgetArea, dock);
     viewMenu-&gt;addAction(dock-&gt;toggleViewAction());

     dock = new QDockWidget(tr(&quot;Paragraphs&quot;), this);
     paragraphsList = new QListWidget(dock);
     paragraphsList-&gt;addItems(QStringList()
             &lt;&lt; &quot;Thank you for your payment which we have received today.&quot;
             &lt;&lt; &quot;Your order has been dispatched and should be with you &quot;
                &quot;within 28 days.&quot;
             &lt;&lt; &quot;We have dispatched those items that were in stock. The &quot;
                &quot;rest of your order will be dispatched once all the &quot;
                &quot;remaining items have arrived at our warehouse. No &quot;
                &quot;additional shipping charges will be made.&quot;
             &lt;&lt; &quot;You made a small overpayment (less than $5) which we &quot;
                &quot;will keep on account for you, or return at your request.&quot;
             &lt;&lt; &quot;You made a small underpayment (less than $1), but we have &quot;
                &quot;sent your order anyway. We'll add this underpayment to &quot;
                &quot;your next bill.&quot;
             &lt;&lt; &quot;Unfortunately you did not send enough money. Please remit &quot;
                &quot;an additional $. Your order will be dispatched as soon as &quot;
                &quot;the complete amount has been received.&quot;
             &lt;&lt; &quot;You made an overpayment (more than $5). Do you wish to &quot;
                &quot;buy more items, or should we return the excess to you?&quot;);
     dock-&gt;setWidget(paragraphsList);
     addDockWidget(Qt::RightDockWidgetArea, dock);
     viewMenu-&gt;addAction(dock-&gt;toggleViewAction());

     connect(customerList, SIGNAL(currentTextChanged(const QString &amp;)),
             this, SLOT(insertCustomer(const QString &amp;)));
     connect(paragraphsList, SIGNAL(currentTextChanged(const QString &amp;)),
             this, SLOT(addParagraph(const QString &amp;)));
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
